import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class DioPage extends StatefulWidget {
  const DioPage({super.key});

  @override
  State<DioPage> createState() => _DioPageState();
}

class _DioPageState extends State<DioPage> {
  final dio = Dio();

  String _data = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Flutter App"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: () async {
              //Get请求
              var response = await dio.get('https://pub.dev');
              setState(() {
                _data = "Get请求结果为:${response.data}";
              });
            }, child: const Text("Get请求")),
            ElevatedButton(onPressed: () async {
              //Post请求
              var response = await dio.post(
                'https://www.dtworkroom.com/doris/1/2.0.0/test',
                //请求参数
                data: {'aa': 'bb' * 22},
                //进度监听
                onSendProgress: (int sent, int total) {
                  print("$sent $total");
                },
              );
            }, child: const Text("Post请求")),
            const Divider(),
            const Text("请求结果:"),
            Text(_data,style: const TextStyle(fontSize: 20))
          ],
        ),
      ),
    );
  }
}
